
function Game(data){
	this.id = data.id;
	this.started = data.started;
	this.end = data.end;
	this.winner_id = data.winner_id;
	this.masterSuit = data.masterSuit;	// master suit talon id
	this.type = data.type;
	this.players = data.players;
	this.talonCards =[];
	this.cards = [];
	this.cardsRevealed = [];
	this.lastCardRevealed = {};
	
}



Game.prototype.isTheSameWithMasterSuit = function(oldCard,newCard){
	var ol = this.cards.getCardByTalonId_(this.talonCards.getCardById(oldCard).card_id);
	var nw = this.cards.getCardByTalonId_(this.talonCards.getCardById(newCard).card_id);
	if(ol.suit==nw.suit){
		return true;
	}
	return false;
}

Game.prototype.isSuitTheSame = function(oldCard,newCard){
	var old = this.cards.getCardByTalonId_(oldCard);
	var newC = this.cards.getCardByTalonId_(newCard);
	if(old.suit==newC.suit){
		return true;
	}
	return false;
}

Game.prototype.removeAllRevealedCards = function(){
	var removeAllRevealedCards = 'index.php?/game/removeAllRevealedCards';
	$.ajax({
		url: removeAllRevealedCards, 
		async:false,
		success: function() {
		// well i have no bussiness here..
	}
	}).done(function(){
		// update talons cards that revealed cards now is not active;
		var revealedCards = game.cardsRevealed;
		 // empty array
		game.talonCards.updateRevealedCards(revealedCards);
		game.cardsRevealed = new Array();
	});

}

Game.prototype.popCard = function(){
	this.cards.pop();
}

Game.prototype.drawCardTo = function(playerId){
	if(this.id==0){
		alert('No active Game session please create new one.');
		return table;
	}
	
	table.addTracerInfo(new Info({'description':'querying player'+playerId+" cards on hand",'triggered':'drawCardTo()'}));
	var replenishePlayerUrl = "index.php?/player/replenishePlayer/"+playerId;
	$.ajax({
		url: replenishePlayerUrl,
		dataType: 'json',
		async:false,
		success: function(card) {
		if(card==null){
			return;
		}
		if(card.masterSuit==true){
			$('#masterSuit').hide();
		}
			game.talonCards.updateTalonCard(card.id,playerId);// inform the deck cards that this card is drawn by this player
			table.appenCardToPlayer(card.id,playerId);//transfer it to players table
			//find('td:nth-child(1)').css('margin-top','0p');
		}
	}).done(function(){
		//table.reloadPlayer(playerId);
		//game.reloadUI();
		//game.refreshGame();
		
	});
	table.addTracerInfo(new Info({'description':'querying player'+playerId+" cards on hand[DONE]",'triggered':'drawCardTo()'}));
	return table;
}



Game.prototype.createNewGameAJAX = function(){
	//createNewGame
	console.log("createNewGameAJAX----------");
	var createNewGameUrl = "index.php?/game/createNewGame";
	
	$.ajax({
		url:createNewGameUrl,
		async:false,
		success:function(response,status){
		Game.prototype.id = response;
		console.log("New game Id: "+Game.prototype.id);
	}
	
	});
	
	//this.getNewSetOfShuffledCards();
	//this.getAllCardsAJAX();
}

Game.prototype.getRevealedCards = function(){
	Turn.prototype.geCards();
}

Game.prototype.createNewMasterSuit = function(){
	console.log('createNewMasterSuit---- starting');
	var createNewMasterSuitUrl = "index.php?/game/createNewMasterSuit";
	$.ajax({
		url:createNewMasterSuitUrl,
		async:false,
		dataType:'json',
		success:function(suit){
		game.masterSuit = suit[0].id;
		game.talonCards.updateMasterSuit(suit[0]);
	}
	
	});
}

Game.prototype.endGame = function(){
	console.log('endGame---- starting');
	var endGameUrl = "index.php?/game/endGame";
	$.ajax({
		url:endGameUrl,
		async:false,
		success:function(){
			window.location = "/durak/index.php?/player";
		}
	});
}

Game.prototype.replenishPlayer = function(playerId){
	var p1C = game.players[0].cards().length;
	console.log('p1C length = '+p1C);
	var p2C = game.players[1].cards().length;
	console.log('p2C length = '+p2C);
	if(playerId==1){
		if(p1C<6){
			var cardsNeeded = 6-p1C;
			console.log('cardsNeeded p1C= '+cardsNeeded);
			for(counter=0;counter<=cardsNeeded;counter++){
				game.drawCardTo(1);
				console.log('cards draw to player 1');
			}
		}
	}else{
		if(p2C<6){
			var cardsNeeded = 6-p2C;
			console.log('cardsNeeded p2C= '+cardsNeeded);
			for(counter1=0;counter1<=cardsNeeded;counter1++){
				console.log('cards draw to player 2');
				game.drawCardTo(2);
			}
		}
	}
	table.updateGameInformation();
}

Game.prototype.replenishPlayers = function (){
	var p1C = game.players[0].cards().length;
	console.log('p1C length = '+p1C);
	var p2C = game.players[1].cards().length;
	console.log('p2C length = '+p2C);
	if(p1C<6){
		var cardsNeeded = 6-p1C;
		console.log('cardsNeeded p1C= '+cardsNeeded);
		for(counter=0;counter<=cardsNeeded;counter++){
			game.drawCardTo(1);
			console.log('cards draw to player 1');
		}
	}
	if(p2C<6){
		var cardsNeeded = 6-p2C;
		console.log('cardsNeeded p2C= '+cardsNeeded);
		for(counter1=0;counter1<=cardsNeeded;counter1++){
			console.log('cards draw to player 2');
			game.drawCardTo(2);
		}
	}
	table.updateGameInformation();
}

Game.prototype.refreshGame = function(){
console.log('refreshGame-----| game id:'+this.id);
	
	if(this.id!=0){
		console.log('true-----');
		game.getAllCardsAJAX();
		$.when(game.getAllTalonCardsAJAX()).done(function(){
			
			table.reLoadTalon();
			table.reLoadPlayersTable(1);
			table.reLoadPlayersTable(2);
			//game.reloadUI();
			console.log('ui resolved');
		});
		
	}else{
		game.getCurrentGameId();
		game.refreshGame();
	}
	return this;
}

Game.prototype.getCurrentGameId = function(){
	
	console.log('getCurrentGameId-----');
	var getCurrentGameIdUrl = "index.php?/game/getCurrentGameId";
	$.ajax({
		url:getCurrentGameIdUrl,
		async:false,
		success:function(response,status){
		game.id = response;
		console.log("Current game Id: "+game.id);
	}
	
	});

	return this;
}

Game.prototype.killAllData = function(){
	console.log('killAllData-------');
	var killAllDataUrl = "index.php?/game/killAllData";
	$.ajax({
		url:killAllDataUrl,
		async:false,
		success:function(response){
		console.log(response);
	}
	});
}

Game.prototype.printGameInfo = function(){
	console.log('prinGameInfo-----------');
	var prinGameInfoUrl = "index.php?/game/printGameInfo";
	$.ajax({
		url:prinGameInfoUrl,
		async:false,
		success:function(response){
		console.log("info:  "+response);
	}
	});
}

Game.prototype.setCurrentGameId = function(gameId){
	console.log('setCurrentGameId-----');
	var setCurrentGameIdUrl = "index.php?/game/setCurrentGameId/"+gameId;
	$.ajax({
		url:setCurrentGameIdUrl,
		async:false,
		success:function(response){
		console.log("Updated Current game Id: "+response);
	}
	});
}

Game.prototype.getTalonCardsByGameId = function(){
	table.addTracerInfo(new Info({'description':'getting list of cards for talon table','triggered':'getTalonCardsByGameId()'}));
	console.log("getTalonCardsByGameId----------");
	var getTalonCardsByGameIdUrl = "index.php?/talon/getTalonCardsByGameId";
	this.talonCards = new Array();
	//talonCards = [];
	console.log('starting ajax');
	table.addTracerInfo(new Info({'description':'starting ajax','triggered':'getTalonCardsByGameId()'}));
	
	$.ajax({
		url:getTalonCardsByGameIdUrl,
		dataType:'json',
		async:false,
		success: function( data , status){
			$(data).each(function(key,talonCard){
				game.talonCards.push(new Talon(talonCard));
			});
		}
	}).done(function(){
		table.addTracerInfo(new Info({'description':'done ajax','triggered':'getTalonCardsByGameId()'}));
		//game.reloadUI();
		console.log('done ajax');
	});
	//this.talonCards = talonCards;
	//table.addTracerInfo(new Info({'description':'Total number of taloncards='+talonCards.getLength(),'triggered':'getTalonCardsByGameId()'}));
	
	return this;
}

Game.prototype.getNewSetOfShuffledCards = function(){
	console.log("getNewSetOfShuffledCards----------");
	var getAllTalonCardsUrl = "index.php?/talon/getNewSetOfShuffledCards";
	$.ajax({
		url:getAllTalonCardsUrl,
		dataType:'json',
		async:false,
		success:function(data){
		$(data).each(function(key,talonCard){
			game.talonCards.push(new Talon(talonCard));
			console.log(talonCard);
		});
	}
	});
	
}


Game.prototype.endGameAJAX = function(){
	//endGame
	var endGameUrl = "index.php?/game/endGame";
	$.ajax({
		url:endGameUrl,
		dataType:'json',
		async:false,
		success:function(gameResult){
		//TODO: handle end of game... 
		
		}
	});
}

Game.prototype.cardsRevealedAJAX = function(){
	//cardsRevealed
	var cardsRevealedUrl = "index.php?/game/cardsRevealed";
	cardsRevealed = [];
	$.ajax({
		url:cardsRevealedUrl,
		async:false,
		dataType:'json',
		success:function(data){
			$.each(data,function(key,cardTurn){
				cardsRevealed.push(new Turn(cardTurn));
			});
		}
	});
	this.cardsRevealed = cardsRevealed;
}

Game.prototype.getAllTalonCardsAJAX = function(){
	console.log("getAllTalonCardsAJAX----------");
	var getAllTalonCardsUrl = "index.php?/talon/getAllTalonCardByGameId";
	$.ajax({
		url:getAllTalonCardsUrl,
		dataType:'json',
		async:false,
		success:function(data){
		$(data).each(function(key,talonCard){
			game.talonCards.push(new Talon(talonCard));
		//	console.log(talonCard);
		});
	}
	});
	return this;
}

Game.prototype.reloadUI = function(){
	$.when(table.reLoadTalon()).then(function(t){
		$.when(t.reloadPlayer(1)).then(function(x){
			$.when(x.reloadPlayer(2)).then(function(y){
				y.reloadRevealedTable();// not done
				console.log('done loading player cards');
			});
		});
	});//
}

Game.prototype.getAllPlayersAJAX = function(){
	var getPlayersUrl = "index.php?/player/getPlayers";
	var players = [];
	$.ajax({
		url: getPlayersUrl, 
		dataType: 'json',
		async:false,
		success: function(data) {	
		console.log("getAllPlayers()");
		$(data).each(function(key,player){
			players.push(new Player(player));
		});
	}
	});
	this.players = players
}

Game.prototype.getAllCardsAJAX = function(){
	console.log('getAllCardsAJAX////');
	var getAllCardsUrl = "index.php?/cards/getAllCards";
	cards = [];
	$.ajax({
		url:getAllCardsUrl,
		dataType:'json',
		async:false,
		success:function(data){
		$(data).each(function(key,card){
			cards.push(new Card(card));
		//	console.log(card.id);
		});
	}
	});
	this.cards = cards;
	return this;
}


